package acceptTests;

import java.io.IOException;
import java.util.Iterator;

import exceptions.database.InvalidUserListException;
import exceptions.database.NonAssessableDatabaseException;
import sessionManager.SessionManager;
import sessionManager.SessionManagerIF;
import user.UserIF;
import user.blog.BlogIF;

public class US07Facade {
	
	private SessionManagerIF session;
	
	public US07Facade() {
		session = SessionManager.getInstance();
	}
	
	public void loadData() throws NonAssessableDatabaseException {
		session.loadData();
	}
	
	public String logon(String login, String password) throws Exception {
		return session.loginUser(login, password);
	}
	
	public int getNumberOfBlogsByLogin(String login) throws Exception {
		return session.getUserByLogin(login).getBlogs().size();
	}
	
	public int getNumberOfBlogsBySessionId(String sessionId) throws Exception {
		return session.getUser(sessionId).getBlogs().size();
	}
	public String getBlogBySessionId(String sessionId, Integer index) throws Exception {
		return session.getUser(sessionId).getBlogs().get(index).getId();
	}
	
	public String getBlogByLogin(String login, Integer index) throws Exception{
		return session.getUserByLogin(login).getBlogs().get(index).getId();
	}
	
	public Integer getNumberOfPosts(String blogId) {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		boolean foundIt = false;
		BlogIF blog = null;
		while (itUsers.hasNext() && !foundIt) {
			UserIF tmpUser = itUsers.next();
			try {
				blog = tmpUser.getBlog(blogId);
				foundIt = true;
			} catch (Exception e) {
				// Nothign here.
			}
		}
		return blog.getPostManager().getNumberOfPosts();
	}
	
	public String getPost(String blogId, int index) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		boolean foundIt = false;
		BlogIF blog = null;
		while (itUsers.hasNext() && !foundIt) {
			UserIF tmpUser = itUsers.next();
			try {
				blog = tmpUser.getBlog(blogId);
				foundIt = true;
			} catch (Exception e) {
				// Nothign here.
			}
		}
		return blog.getPostManager().getPost(index).getId();
	}
	
	public void changePostInformation(String sessionId, String postId, String flag, String newValue) throws Exception {
		UserIF user = session.getUser(sessionId);
		Iterator<BlogIF> itBlogs = user.getBlogs().iterator();
		while (itBlogs.hasNext() ) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostByPID(postId) != null) {
				tmpBlog.getPostManager().setPostInformation(postId, flag, newValue);
			}
		}
	}
	
	public int getNumberOfSounds(String postId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfAudios();
				}
			}
		}
		return -1;
	}
	
	public int getNumberOfMovies(String postId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfVideos();
				}
			}
		}
		return -1;
	}
	
	public int getNumberOfPictures(String postId) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();
		
		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getNumberOfImages();
				}
			}
		}
		return -1;
	}
	
	public String getMovie(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getVideo(index).getId();
				}
			}
		}
		
		return null;
	}
	
	public void deleteMovie(String sessionId, String movieId) throws Exception {
		UserIF user = session.getUser(sessionId);
		
		Iterator<BlogIF> itBlogs= user.getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostManager().getMedia(movieId) != null) {
				tmpBlog.getPostManager().removeVideo(movieId);
			}
		}
	}
	
	public String getSound(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getAudio(index).getId();
				}
			}
		}
		
		return null;
	}
	
	public void deleteSound(String sessionId, String soundId) throws Exception {
		UserIF user = session.getUser(sessionId);
		
		Iterator<BlogIF> itBlogs= user.getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostManager().getMedia(soundId) != null) {
				tmpBlog.getPostManager().removeAudio(soundId);
			}
		}
	}
	
	public String getPicture(String postId, int index) throws Exception {
		Iterator<UserIF> itUser = session.getUsers().iterator();

		while (itUser.hasNext()) {
			UserIF tmpUser = itUser.next();

			Iterator<BlogIF> itBlogs = tmpUser.getBlogs().iterator();

			while (itBlogs.hasNext()) {
				BlogIF tmpBlog = itBlogs.next();
				if (tmpBlog.getPostByPID(postId) != null) {
					return tmpBlog.getPostByPID(postId).getImage(index).getId();
				}
			}
		}
		
		return null;
	}
	
	public void deletePicture(String sessionId, String pictureId) throws Exception {
		UserIF user = session.getUser(sessionId);
		
		Iterator<BlogIF> itBlogs= user.getBlogs().iterator();
		
		while(itBlogs.hasNext()) {
			BlogIF tmpBlog = itBlogs.next();
			if(tmpBlog.getPostManager().getMedia(pictureId) != null) {
				tmpBlog.getPostManager().removeImage(pictureId);
			}
		}
	}
	
	public void logoff(String sessionId) throws Exception {
		session.logoffUser(sessionId);
	}
	
	public void saveData() throws InvalidUserListException, IOException {
		session.saveData();
	}
}